<template>
  <div>
    <el-form v-model="formSearch" :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formSearch.content" placeholder="请输入搜索内容"></el-input>
      </el-form-item>
      <el-form-item label="搜索类型">
        <el-select v-model="formSearch.type" placeholder="默认">
          <el-option label="用户名" value="username"></el-option>
          <el-option label="邮箱" value="email"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="showAddUser" plain>新增</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="delBatch" plain>批量删除</el-button>
    </el-row>

    <el-table :data="userList"
              v-loading="loading"
              style="width: 100%; margin-top: 20px"
              border
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column prop="id"
                       label="ID"
                       sortable
                       width="180">
      </el-table-column>
      <el-table-column prop="username"
                       label="用户名"
                       width="180">
      </el-table-column>
      <el-table-column prop="email"
                       label="邮箱">
      </el-table-column>
      <el-table-column prop="phone"
                       label="手机号">
      </el-table-column>
      <!--      <el-table-column prop="role"
                             label="角色">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.role === 0" type="success">管理员</el-tag>
                <el-tag v-if="scope.row.role === 1" type="info">普通用户</el-tag>
              </template>
            </el-table-column>-->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="showEditUser(scope.row)" circle></el-button>
          <el-button type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" circle></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px"
        background
        :page-sizes="[5, 10]"
        layout="sizes, prev, pager, next"
        :total="total"
        :current-page="currentPage"
        :page-size="pageSize"
    >
    </el-pagination>

    <!--        新增用户-->
    <el-dialog title="新增用户" :visible.sync="addDialogFormVisible" width="600px" class="demo-ruleForm">
      <el-form :model="addForm" :rules="rules">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="addForm.username" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="addForm.password" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="addForm.email" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="addForm.phone" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select multiple v-model="addForm.roleIds" placeholder="请选择角色" @change="$forceUpdate()">
            <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
                :disabled="item.status === 2"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddSure">确 定</el-button>
      </div>
    </el-dialog>

    <!--        编辑用户-->
    <el-dialog title="编辑用户" :visible.sync="editDialogFormVisible" width="600px" class="demo-ruleForm">
      <el-form :model="editForm" :rules="rules">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="editForm.username" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="editForm.email" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="editForm.phone" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">                   <!--使用vue中自带的方法：强制刷新渲染-->
          <el-select multiple v-model="editForm.roleIds" placeholder="请选择角色" @change="$forceUpdate()">
            <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
                :disabled="item.status === 2"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default ({
  name: 'admin-user',
  data() {
    return {
      userList: [], //用户列表
      formSearch: { //搜索框表单
        content: '',
        type: ''
      },
      pageSize: 5,
      total: 0,
      currentPage: 0,
      multipleSelection: [],
      addDialogFormVisible: false, //添加用户弹框
      editDialogFormVisible: false, //编辑用户弹框
      addForm: {}, //添加用户信息
      editForm: {}, //编辑用户信息
      roleOptions: [], // 角色选项
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ]
      },
      formLabelWidth: '120px',
      loading: true,
    }
  },
  mounted() {
    this.getUserList()
  },
  methods: {
    getUserList() {
      this.loading = true
      this.request.get('/user/list', {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          content: this.formSearch.content,
          type: this.formSearch.type
        }
      }).then((res) => {
        this.userList = res.list
        this.total = Number(res.total)
        this.loading = false
      }).catch(e => {
        this.$message.error(e)
      })
    },

    //打开编辑用户弹框
    showEditUser(row) {
      this.editDialogFormVisible = true
      this.editForm = {}
      this.getUserOne(row.id)
    },
    //获取单个用户的信息
    getUserOne(id) {
      this.request.get('/user/' + id).then((res) => {
        this.editForm = res.userVo
        this.roleOptions = res.roleVos
        this.editForm.roleIds = res.roleIds
      }).catch(() => {
        this.$message.error('查询失败，请重试')
      })
    },
    handleEditSure() {
      this.request.put('/user/update', this.editForm).then(() => {
        this.$message.success("修改成功")
        this.editDialogFormVisible = false
        this.getUserList()
      }).catch(e => {
        this.$message.error(e)
      })
    },
    //删除用户
    handleDelete(row) {
      this.$confirm("是否删除该用户", "提示", {
        type: 'error'
      }).then(() => {
        this.request.delete('/user/' + row.id).then(() => {
          this.$message.success("删除成功")
          location.reload()
        }).catch(e => {
          this.$message.error(e)
        })
      }).catch(() => {
        this.$message.info('取消删除')
      })
    },
    //搜索按钮
    onSubmit() {
      this.getUserList()
    },
    //分页功能
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getUserList()
    },
    //翻页功能
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.getUserList()
    },
    //批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delBatch() {
      this.$confirm("是否确认批量删除", "提示", {
        type: 'error'
      }).then(() => {
        let ids = this.multipleSelection.map(v => v.id);
        if (ids.length > 0) {
          this.request.delete('/user/' + ids).then(() => {
            this.$message.success('批量删除成功')
            location.reload()
          }).catch(e => {
            this.$message.error(e)
            location.reload()
          })
        } else {
          this.$message.info('当前未选中用户')
        }

      }).catch(() => {
        this.$message.info('取消删除')
      })
    },

    //打开添加用户弹框
    showAddUser() {
      this.addDialogFormVisible = true
      this.addForm = {};
      this.request.get('/role/listAllRole').then((res) => {
        this.roleOptions = res
      }).catch(e => {
        this.$message.error(e)
      })
    },
    handleAddSure() {
      console.log(this.addForm)
      this.request.post('/user/add', this.addForm).then(() => {
        this.$message.success('添加成功')
        this.addDialogFormVisible = false
        this.getUserList()
      }).catch(e => {
        this.$message.error(e)
      })
    }
  }
})

</script>

<style scoped>

</style>